/**
 * 违章登记 编辑页
 *
 * 作者 wanghw
 * 日期 2024-08-12 09:46:29
 */
layui.use(['jquery','febs', 'form', 'laydate', 'validate', 'fileUpload','autocomplete','commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#peccancyInformation-edit'),
        laydate = layui.laydate,
        commonJS = layui.commonJS,
        formSelects = layui.formSelects,
        autocomplete = layui.autocomplete,
        $addBtn = $view.find("#addBtn"),
        $deptNameAddBtn = $view.find("#deptNameAddBtn"),
        $contractorAddBtn = $view.find("#contractorAddBtn"),
        $addClauseBtn = $view.find("#addClauseBtn"),
        $peccancyOperation = $view.find("#jobOperation"),
        $peccancyType = $view.find("#peccancyType"),
        $peccancyRootCause = $view.find("#peccancyRootCause"),
        $peccancyUserSign = $view.find("#peccancyUserSign"),
        fileUpload = layui.fileUpload,
        validate = layui.validate,
        auditTableIns,
        issuanceTableIns;


    initProject();
    initDate();
    initFormValue();

    form.verify(validate);
    form.render();

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'peccancy/upload',
        type: 0,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        done: function (res) {
        },
    });


    function initFormValue() {
        if (peccancyInformationData) {
            febs.initDictItemSelect($peccancyType,'SAFETY_PECCANCY_TYPE',peccancyInformationData.peccancyType,null);
            febs.initDictItemSelect($peccancyOperation,'SAFETY_JOB_OPERATION',peccancyInformationData.jobOperation,null);
            febs.initDictItemSelect($peccancyRootCause,'SAFETY_PECCANCY_ROOT_REASON',peccancyInformationData.peccancyRootCause,null);
            initProjectAreaId(peccancyInformationData.projectId);

            form.val("peccancyInformation-edit-form", {
                "informationId": peccancyInformationData.informationId,
                "tenantId": peccancyInformationData.tenantId,
                "peccancyNo": peccancyInformationData.peccancyNo,
                "projectId": peccancyInformationData.projectId,
                "projectName": peccancyInformationData.projectName,
                "businessType": peccancyInformationData.businessType,
                "clauseId": peccancyInformationData.clauseId,
                "clauseName": peccancyInformationData.clauseName,
                "clauseRuleId": peccancyInformationData.clauseRuleId,
                "clauseRuleContent": peccancyInformationData.clauseRuleContent,
                "disposeForm": peccancyInformationData.disposeForm,
                "peccancyType": peccancyInformationData.peccancyType,
                "jobOperation": peccancyInformationData.jobOperation,
                "peccancyRootCause": peccancyInformationData.peccancyRootCause,
                "peccancyAreaId": peccancyInformationData.peccancyAreaId,
                "peccancyArea": peccancyInformationData.peccancyArea,
                "peccancyTime": peccancyInformationData.peccancyTime,
                "reason": peccancyInformationData.reason,
                "peccancyUserId": peccancyInformationData.peccancyUserId,
                "peccancyUserNo": peccancyInformationData.peccancyUserNo,
                "peccancyRealName": peccancyInformationData.peccancyRealName,
                "peccancyUserIdentificationNo": peccancyInformationData.peccancyUserIdentificationNo,
                "peccancyUserPostId": peccancyInformationData.peccancyUserPostId,
                "peccancyUserPostName": peccancyInformationData.peccancyUserPostName,
                "peccancyUserDeptId": peccancyInformationData.peccancyUserDeptId,
                "peccancyUserDept": peccancyInformationData.peccancyUserDept,
                "peccancyUserContractorId": peccancyInformationData.peccancyUserContractorId,
                "peccancyUserContractorName": peccancyInformationData.peccancyUserContractorName,
                "peccancyUserContractorTeamId": peccancyInformationData.peccancyUserContractorTeamId,
                "peccancyUserContractorTeamName": peccancyInformationData.peccancyUserContractorTeamName,
                "peccancyUserSign": peccancyInformationData.peccancyUserSign,
                "peccancyNature": peccancyInformationData.peccancyNature,
                "status": peccancyInformationData.status,
                "peccancyObject": peccancyInformationData.peccancyObject,
                "finallyPenaltyAmount": peccancyInformationData.finallyPenaltyAmount,
            });
            if(peccancyInformationData.tenantName){
                $('#tenantName').text(peccancyInformationData.tenantName);
            }

            if(peccancyInformationData.peccancyUserIdentificationNo){
                $('#idFlg').removeClass('febs-hide');
                $('#peccancyUserIdentificationNo').text(peccancyInformationData.peccancyUserIdentificationNo);
            }

            if(peccancyInformationData.peccancyUserNo){
                $('#userNoFlg').removeClass('febs-hide');
                $('#peccancyUserNo').text(peccancyInformationData.peccancyUserNo);
            }
            if(peccancyInformationData.peccancyUserDept){
                $('#deptFlg').removeClass('febs-hide');
                $('#peccancyUserDept').text(peccancyInformationData.peccancyUserDept);
            }
            if(peccancyInformationData.peccancyUserPostName){
                $('#postFlg').removeClass('febs-hide');
                $('#peccancyUserPostName').text(peccancyInformationData.peccancyUserPostName);
            }
            if(peccancyInformationData.peccancyUserContractorName){
                $('#contractorFlg').removeClass('febs-hide');
                $('#peccancyUserContractorName').text(peccancyInformationData.peccancyUserContractorName);
            }
            if(peccancyInformationData.peccancyUserContractorTeamName){
                $('#peccancyUserContractorTeamName').text(peccancyInformationData.peccancyUserContractorTeamName);
            }
            if(peccancyInformationData.clauseRuleContent){
                $('#clauseRuleContent').text(peccancyInformationData.clauseRuleContent);
            }

            if(peccancyInformationData.finallyPenaltyAmount){
                $('#finallyPenaltyAmount').text(peccancyInformationData.finallyPenaltyAmount);
            }

            if(peccancyInformationData.disposeForm){
                var disposeFormName = febs.getDictLabel(disposeFormList,peccancyInformationData.disposeForm);
                $('#disposeFormName').text(disposeFormName);
            }

            if(peccancyInformationData.isScene == '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
                form.render();
            }

            if(peccancyInformationData.penaltyScoreFlg && peccancyInformationData.penaltyScoreFlg == '1'){
                $view.find('input[name="penaltyScore"]').removeAttr('disabled');
                $view.find('input[name="penaltyScore"]').val(peccancyInformationData.penaltyScore);
                $view.find('input[name="penaltyScoreFlg"]').attr('checked','true');
            }
            if(peccancyInformationData.learnVideoFlg && peccancyInformationData.learnVideoFlg =='1'){
                $view.find('input[name="learnVideoFlg"]').attr('checked','true');
            }

            if(peccancyInformationData.penaltyAmountFlg && peccancyInformationData.penaltyAmountFlg =='1'){
                $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
                $view.find('input[name="penaltyAmount"]').val(peccancyInformationData.penaltyAmount);
                $view.find('input[name="penaltyAmountFlg"]').attr('checked','true');
            }

            if(peccancyInformationData.peccancyObject == '4'){
                //承包队
                $('#userPeccency').addClass('febs-hide');
                $('#deptPeccency').addClass('febs-hide');
                $('#objectUser').addClass('febs-hide');
                $('#objectDept').addClass('febs-hide');
                $('#objectContractor').removeClass('febs-hide');
                $('#contractorPeccency').removeClass('febs-hide');
                $('#peccancyContractorName').attr('lay-verify','true');
                $('#peccancyRealName').attr('lay-verify','');
                $('#peccancyDept').attr('lay-verify','');
                $view.find('input[name="peccancyContractorName"]').val(peccancyInformationData.peccancyUserContractorTeamName);
                getPeccancyCount(peccancyInformationData.peccancyUserContractorTeamId,peccancyInformationData.peccancyObject);

            }else if(peccancyInformationData.peccancyObject == '5'){
                //部门
                $('#userPeccency').addClass('febs-hide');
                $('#deptPeccency').removeClass('febs-hide');
                $('#contractorPeccency').addClass('febs-hide');

                $('#objectUser').addClass('febs-hide');
                $('#objectDept').removeClass('febs-hide');
                $('#objectContractor').addClass('febs-hide');

                $('#peccancyContractorName').attr('lay-verify','');
                $('#peccancyRealName').attr('lay-verify','');
                $('#peccancyDept').attr('lay-verify','true');
                $view.find('input[name="peccancyDept"]').val(peccancyInformationData.peccancyUserDept);
                getPeccancyCount(peccancyInformationData.peccancyUserDeptId,peccancyInformationData.peccancyObject);
            }else {
                $('#objectUser').removeClass('febs-hide');
                $('#objectDept').addClass('febs-hide');
                $('#objectContractor').addClass('febs-hide');

                $('#userPeccency').addClass('febs-hide');
                $('#deptPeccency').addClass('febs-hide');
                $('#contractorPeccency').addClass('febs-hide');

                $('#peccancyContractorName').attr('lay-verify','');
                $('#peccancyRealName').attr('lay-verify','true');
                $('#peccancyDept').attr('lay-verify','');
                getPeccancyCount(peccancyInformationData.peccancyUserId,peccancyInformationData.peccancyObject);
            }

            if(peccancyInformationData.auditList){
                auditTableIns = febs.table.init({
                    elem: $view.find('table[id="peccancyAuditRecordTable"]'),
                    id: 'peccancyAuditRecordTable',
                    page:false,
                    skin: '',
                    limit:Number.MAX_VALUE,
                    cols: [
                        [
                            {type: 'numbers',title:'序号'},
                            {field: 'auditRealName', title: '审核人姓名', minWidth: 80},
                            {
                                field: 'auditResult', title: '审核结果', width: 100, align: 'center', templet: function (d) {
                                    if (d.auditResult == '1') {
                                        return '<span class="layui-badge febs-tag-green">审核通过</span>';
                                    } else {
                                        return '<span class="layui-badge febs-tag-magenta">审核退回</span>';
                                    }
                                }
                            },
                            {field: 'auditTime', title: '审核时间', width: 160,align:'center'},
                            {field: 'auditReason', title: '审核理由', minWidth: 120 }
                        ]
                    ],
                    data:peccancyInformationData.auditList
                });
            }

            if(peccancyInformationData.issuanceList){
                issuanceTableIns = febs.table.init({
                    elem: $view.find('table[id="peccancyIssuanceRecordTable"]'),
                    id: 'peccancyIssuanceRecordTable',
                    page:false,
                    skin: '',
                    limit:Number.MAX_VALUE,
                    cols: [
                        [
                            {type: 'numbers',title:'序号'},
                            {field: 'auditRealName', title: '签发人姓名', minWidth: 80},
                            {
                                field: 'auditResult', title: '签发结果', width: 100, align: 'center', templet: function (d) {
                                    if (d.auditResult == '1') {
                                        return '<span class="layui-badge febs-tag-green">签发通过</span>';
                                    } else {
                                        return '<span class="layui-badge febs-tag-magenta">签发退回</span>';
                                    }
                                }
                            },
                            {field: 'auditTime', title: '签发时间', width: 160,align:'center'},
                            {field: 'auditReason', title: '签发理由', minWidth: 120 },
                        ]
                    ],
                    data:peccancyInformationData.issuanceList
                });
            }

            if(peccancyInformationData.peccancyUserSign){
                $view.find('#peccancyUserSignImg').removeClass('febs-hide');
                $view.find('#peccancyUserSignImg1').attr('src','/'+peccancyInformationData.peccancyUserSign);
            }

        }else {
            form.val("peccancyInformation-edit-form", {
                "tenantId": POPUP_DATA.tenantId,
            });
            febs.initDictItemSelect($peccancyType,'SAFETY_PECCANCY_TYPE',null,null);
            febs.initDictItemSelect($peccancyOperation,'SAFETY_JOB_OPERATION',null,null);
            febs.initDictItemSelect($peccancyRootCause,'SAFETY_PECCANCY_ROOT_REASON',null,null);
            $('#tenantName').text(POPUP_DATA.tenantName);
        }
        form.render();
    }

    function getFileList() {
        if (peccancyInformationData && peccancyInformationData.fileList.length>0) {
            return  peccancyInformationData.fileList;
        }else {
            return  [];
        }
    }


    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#febs-form-group-date',
            // type: 'datetime',
            value: new Date(),
            format:'yyyy-MM-dd',
            isInitValue: true,
            btns:['now','confirm']
        })
    }

    form.on('submit(peccancyInformation-edit-form-submit)', function (data) {
        data.field.status = '2';
        data.field.fileList = getImgData();
        // console.log("length"+data.field.fileList.length)
        if(data.field.fileList.length == 0){
            febs.alert.warn('提交请上传违章图片');
            return false;
        }
        if(data.field.isScene == 'on'){
            data.field.isScene = '1';
        }else {
            data.field.isScene = '0';
        }
        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }
        addOrUpdate(data.field);
        return false;
    });


    form.on('submit(peccancyInformation-edit-form-save)', function (data) {
        data.field.status = '1';
        data.field.fileList = getImgData();
        if(data.field.isScene == 'on'){
            data.field.isScene = '1';
        }else {
            data.field.isScene = '0';
        }
        if(data.field.fileList.length == 0){
            febs.alert.warn('保存请上传违章图片');
            return false;
        }

        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }

        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        var msg1 = '保存提示！';
        var msg2 = '是否保存？';
        if(data.status == '2'){
            msg1 = '提交提示！';
            msg2 = '是否提交？';
        }
        febs.modal.confirm(msg1,msg2,function (index){
            if (data.informationId && data.informationId.length > 0) {
                febs.postArray(ctx + 'peccancyInformation/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-peccancyInformation').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'peccancyInformation', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-peccancyInformation').find('#query').trigger('click');
                });
            }
        });

    }

    function initProject() {
        //初始化项目
        var data ={
            tenantId:getTenantId(),
            status:'1'
        };
        //初始化商品单位
        febs.getSync(ctx + 'baseProject', data, function (r) {
            // console.log("项目列表" + JSON.stringify(r.data))
            var areaLis = r.data;
            var options = '<option value="">请选择</option>';
            $.each(areaLis, function (index, item) {
                options += '<option value="' + item.projectId + '">' + item.projectName + '</option>'
            })
            $("#projectId").append(options);
        });
        form.render();
    }

    form.on('select(projectId)', function(obj){
        // 获取选中项 value
        var projectId = obj.value;
        var projectName = $view.find('select[name="projectId"]').find('option:selected').text().trim();
        // console.log("projectId"+projectId+'projectName'+projectName)
        if(projectId == ''){
            $view.find('input[name="projectName"]').val('');
            $view.find('input[name="peccancyArea"]').val('');
            // $('#peccancyAxis').text('--');
            $("#peccancyAreaId").empty();
        }else {
            $view.find('input[name="projectName"]').val(projectName);
            // $('#peccancyAxis').text('--');
            initProjectAreaId(projectId)

        }

        form.render();
    });


    function initProjectAreaId(projectId) {
        //初始化项目区域
        var data ={
            tenantId:getTenantId(),
            projectId:projectId
        };
        $("#peccancyAreaId").empty();
        febs.getSync(ctx + 'baseProjectArea', data, function (r) {
            // console.log("区域列表" + JSON.stringify(r.data))
            var areaLis = r.data;
            var options = '<option value="">请选择</option>';
            $.each(areaLis, function (index, item) {
                options += '<option value="' + item.areaId + '">' + item.areaName + '</option>'
            })
            $("#peccancyAreaId").append(options);
        });
    }

    form.on('select(peccancyAreaId)', function(obj){
        var areaId = obj.value;
        if(areaId == ''){
            $view.find('input[name="peccancyArea"]').val('');
            // $('#peccancyAxis').text('--');
        }else {
            febs.getSync(ctx + 'baseArea/get/'+areaId, null, function (r) {
                // console.log("区域详情" + JSON.stringify(r.data))
                var area = r.data;
                $view.find('input[name="peccancyArea"]').val(area.areaName);
                // $('#peccancyAxis').text(area.areaCoordinate);
            });
        }
        form.render();
    });

    function getTenantId(){
        if(peccancyInformationData){
            return  peccancyInformationData.tenantId;
        }else {
            return POPUP_DATA.tenantId;
        }
    }

    function getPeccancyObject(){
        return $view.find('input[name="peccancyObject"]:checked').val();
    }


    $addClauseBtn.on('click', function () {

        if(peccancyInformationData && peccancyInformationData.penaltyAmountStatus=='1'){
            febs.alert.success("该人员已缴纳过罚款，禁止重新选择违章条款！");
            return;
        }

        var peccancyObject = getPeccancyObject()
        if(peccancyObject == '4'){
            //承包队
            var peccancyUserContractorName =  $view.find('input[name="peccancyUserContractorName"]').val();
            if(commonJS.isEmpty(peccancyUserContractorName)){
                febs.alert.success("请先选择违章承包队！");
                return;
            }
        }else if(peccancyObject == '5'){
            //部门
            var peccancyDept =  $view.find('input[name="peccancyDept"]').val();
            if(commonJS.isEmpty(peccancyDept)){
                febs.alert.success("请先选择违章部门！");
                return;
            }
        }else {
            //人
            var userName =  $view.find('input[name="peccancyRealName"]').val();
            if(commonJS.isEmpty(userName)){
                febs.alert.success("请先选择违章人员！");
                return;
            }
        }

        febs.modal.open('选择违章条款', 'common/peccancyClause', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['70%', '85%'],
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                // console.log('选择违章条款'+JSON.stringify(selectData));
                $view.find('#disposeFormName').text(selectData.disposeFormName);
                $view.find('input[name="clauseName"]').val(selectData.clauseName);
                $view.find('input[name="clauseId"]').val(selectData.clauseId);
                $view.find('input[name="disposeForm"]').val(selectData.disposeForm);
                $view.find('input[name="clauseRuleId"]').val(selectData.clauseRuleId);
                $view.find('input[name="clauseRuleContent"]').val(selectData.clauseContent);
                $('#clauseRuleContentItem').removeClass('febs-hide');
                $('#clauseRuleContent').text(selectData.clauseContent);
                $('#finallyPenaltyAmount').text(selectData.referenceMoney);
                $view.find('input[name="finallyPenaltyAmount"]').val(selectData.referenceMoney);
                layer.close(index);
            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            },
            data: {
                useScope : peccancyObject,
                tenantId : getTenantId(),
            }
        });

    });

    $('#peccancyRealName').autocomplete({
        serviceUrl: ctx + 'common/user/list',//测试接口
        paramName:'keywords',
        useType:'user',
        params:{ tenantId: getTenantId(),
        userType: getPeccancyObject()},
        onSelect: function(suggestion) {
            $view.find('input[name="peccancyRealName"]').val(suggestion.value);
            $view.find('input[name="peccancyUserId"]').val(suggestion.data);
            getPeccancyCount(suggestion.data,getPeccancyObject())

            if(suggestion.identificationNo){
                $('#idFlg').removeClass('febs-hide');
                $('#peccancyUserIdentificationNo').text(suggestion.identificationNo);
                $view.find('input[name="peccancyUserIdentificationNo"]').val(suggestion.identificationNo);
            }else {
                $('#idFlg').removeClass('febs-hide');
                $('#peccancyUserIdentificationNo').text('--');
                $view.find('input[name="peccancyUserIdentificationNo"]').val('');
            }

            if(suggestion.userNo){
                $('#userNoFlg').removeClass('febs-hide');
                $('#peccancyUserNo').text(suggestion.userNo);
                $view.find('input[name="peccancyUserNo"]').val(suggestion.userNo);
            }else {
                $('#userNoFlg').removeClass('febs-hide');
                $('#peccancyUserNo').text('--');
                $view.find('input[name="peccancyUserNo"]').val('');

            }

            if(suggestion.deptId){
                $('#deptFlg').removeClass('febs-hide');
                $('#peccancyUserDept').text(suggestion.deptName);
                $view.find('input[name="peccancyUserDeptId"]').val(suggestion.deptId);
            }else {
                $('#deptFlg').removeClass('febs-hide');
                $('#peccancyUserDept').text('--');
                $view.find('input[name="peccancyUserDeptId"]').val('');
            }

            if(suggestion.postId){
                $('#postFlg').removeClass('febs-hide');
                $view.find('input[name="peccancyUserPostId"]').val(suggestion.postId);
                $('#peccancyUserPostName').text(suggestion.postName);
            }else {
                $('#postFlg').removeClass('febs-hide');
                $view.find('input[name="peccancyUserPostId"]').val('');
                $('#peccancyUserPostName').text('--');
            }

            if(suggestion.userType == '2'){
                $('#contractorFlg').removeClass('febs-hide');

            }else {
                $('#contractorFlg').addClass('febs-hide');
            }

            if(suggestion.contractorId){
                $('#peccancyUserContractorName').text(suggestion.contractorName);
                $view.find('input[name="peccancyUserContractorId"]').val(suggestion.contractorId);
            }else {
                $view.find('input[name="peccancyUserContractorId"]').val('');
                $('#peccancyUserContractorName').text('--');
            }

            if(suggestion.contractorTeamId){
                $('#peccancyUserContractorTeamName').text(suggestion.contractorTeamName);
                $view.find('input[name="peccancyUserContractorTeamId"]').val(suggestion.contractorTeamId);
            }else {
                $view.find('input[name="peccancyUserContractorTeamId"]').val('');
                $('#peccancyUserContractorTeamName').text('--');
            }
        },
    });

    $addBtn.on('click', function () {
        febs.modal.open('选择用户', 'common/user', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['90%', '85%'],
            data:{
                tenantId:getTenantId(),
                userType:getPeccancyObject(),
                multiple:false,
            },
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                temp = selectData;
                // console.log("获得的人员"+JSON.stringify(temp))
                // debugger

                if(selectData){
                    $('#userPeccency').removeClass('febs-hide');
                    $view.find('input[name="peccancyUserId"]').val(selectData.userId);
                    getPeccancyCount(selectData.userId,getPeccancyObject())
                    $view.find('input[name="peccancyRealName"]').val(selectData.uname);
                    if(selectData.identificationNo){
                        $('#idFlg').removeClass('febs-hide');
                        $('#peccancyUserIdentificationNo').text(selectData.identificationNo);
                        $view.find('input[name="peccancyUserIdentificationNo"]').val(selectData.identificationNo);
                    }else {
                        $('#idFlg').removeClass('febs-hide');
                        $('#peccancyUserIdentificationNo').text('--');
                        $view.find('input[name="peccancyUserIdentificationNo"]').val('');
                    }

                    if(selectData.userNo){
                        $('#userNoFlg').removeClass('febs-hide');
                        $('#peccancyUserNo').text(selectData.userNo);
                        $view.find('input[name="peccancyUserNo"]').val(selectData.userNo);
                    }else {
                        $('#userNoFlg').removeClass('febs-hide');
                        $('#peccancyUserNo').text('--');
                        $view.find('input[name="peccancyUserNo"]').val('');

                    }

                    if(selectData.deptId){
                        $('#deptFlg').removeClass('febs-hide');
                        $('#userDept').text(selectData.deptName);
                        $view.find('input[name="peccancyUserDeptId"]').val(selectData.deptId);
                        $view.find('input[name="peccancyUserDept"]').val(selectData.deptName);
                    }else {
                        $('#deptFlg').removeClass('febs-hide');
                        $('#userDept').text('--');
                        $view.find('input[name="peccancyUserDeptId"]').val('');
                        $view.find('input[name="peccancyUserDept"]').val('');
                    }

                    if(selectData.postId){
                        $('#postFlg').removeClass('febs-hide');
                        $view.find('input[name="peccancyUserPostId"]').val(selectData.postId);
                        $view.find('input[name="peccancyUserPostName"]').val(selectData.postName);
                        $('#userPostName').text(selectData.postName);
                    }else {
                        $('#postFlg').removeClass('febs-hide');
                        $view.find('input[name="peccancyUserPostId"]').val('');
                        $view.find('input[name="peccancyUserPostName"]').val('');
                        $('#userPostName').text('--');
                    }

                    if(selectData.userType == '2'){
                        $('#contractorFlg').removeClass('febs-hide');
                    }else {
                        $('#contractorFlg').addClass('febs-hide');
                    }

                    if(selectData.contractorId){
                        $('#peccancyUserContractorName').text(selectData.contractorName);
                        $view.find('input[name="peccancyUserContractorId"]').val(selectData.contractorId);
                        $view.find('input[name="peccancyUserContractorName"]').val(selectData.contractorName);
                    }else {
                        $view.find('input[name="peccancyUserContractorId"]').val('');
                        $view.find('input[name="peccancyUserContractorName"]').val('');
                        $('#peccancyUserContractorName').text('--');
                    }

                    if(selectData.contractorTeamId){
                        $('#peccancyUserContractorTeamName').text(selectData.contractorTeamName);
                        $view.find('input[name="peccancyUserContractorTeamId"]').val(selectData.contractorTeamId);
                        $view.find('input[name="peccancyUserContractorTeamName"]').val(selectData.contractorTeamName);
                    }else {
                        $view.find('input[name="peccancyUserContractorTeamId"]').val('');
                        $view.find('input[name="peccancyUserContractorTeamName"]').val('');
                        $('#peccancyUserContractorTeamName').text('--');
                    }
                    layer.close(index);
                }

            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });

    });

    /**
     * 获取人员违章统计 本年违章次数，罚款金额 ，扣除分数
     * @param userId
     */
    function getPeccancyCount(userId,peccancyObject){
        var data={

        }
        data.peccancyObject = peccancyObject;
        if(peccancyObject == '4'){
            data.peccancyUserContractorTeamId=userId
        }else if(peccancyObject == '5'){
            data.peccancyUserDeptId=userId
        }else {
            data.peccancyUserId=userId
        }
        febs.get(ctx + 'countPeccancy', data, function (r) {
            // console.log("违章统计详情" + JSON.stringify(r.data))
            var countPeccancy = r.data;
            $('#yearCount').removeClass('febs-hide');
            $('#yearCountNum').text(countPeccancy.yearCount);
            if(countPeccancy.COUNT == 0){
                $('#yearCountMoney').text(0);
                $('#yearCountSocre').text(0);
                $('#yearCountLearn').text(0);
            }else {
                $('#yearCountMoney').text(countPeccancy.amountCount);
                $('#yearCountSocre').text(countPeccancy.scoreCount);
                $('#yearCountLearn').text(countPeccancy.videoCount);
            }
        });
    }

    /**
     * 获取图片
     * @returns {*[]}
     */
    function getImgData(){
        var imgDatas = $view.find(".imgOne")
        var imgList = [];
        $.each(imgDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            imgList.push(data);
        })
        return imgList;
    }

    function clearClause() {
        //清空人员信息
        var peccancyObject = getPeccancyObject()
        if(peccancyObject == '4'){
            //承包队
            $view.find('input[name="peccancyUserDept"]').val('');
        }else if(peccancyObject == '5'){
            //部门
            $view.find('input[name="peccancyUserContractorName"]').val('');
        }else {

        }
         //人

         $view.find('input[name="peccancyRealName"]').val('');
         $view.find('input[name="peccancyUserPostId"]').val('');
         $view.find('input[name="peccancyUserNo"]').val('');
         $view.find('input[name="peccancyUserDeptId"]').val('');
         $view.find('input[name="peccancyUserDept"]').val('');
         $view.find('input[name="peccancyUserContractorId"]').val('');
         $view.find('input[name="peccancyUserContractorTeamId"]').val('');

        $('#yearCount').addClass('febs-hide');
        $('#userPeccency').addClass('febs-hide');
        $view.find('#disposeFormName').text('--');
        $view.find('input[name="clauseName"]').val('');
        $view.find('input[name="clauseId"]').val('');
        $view.find('input[name="disposeForm"]').val('');
        $view.find('input[name="clauseRuleId"]').val('');
    }


    form.on('radio(peccancyObject)', function(obj){
        var peccancyObject = obj.value;
        clearClause();
        if(peccancyObject == '4'){
            //承包队
            $('#userPeccency').addClass('febs-hide');
            $('#deptPeccency').addClass('febs-hide');
            $('#objectUser').addClass('febs-hide');
            $('#objectDept').addClass('febs-hide');
            $('#objectContractor').removeClass('febs-hide');
            $('#contractorPeccency').removeClass('febs-hide');
            $('#peccancyContractorName').attr('lay-verify','true');
            $('#peccancyRealName').attr('lay-verify','');
            $('#peccancyDept').attr('lay-verify','');
        }else if(peccancyObject == '5'){
            //部门
            $('#userPeccency').addClass('febs-hide');
            $('#deptPeccency').removeClass('febs-hide');
            $('#contractorPeccency').addClass('febs-hide');

            $('#objectUser').addClass('febs-hide');
            $('#objectDept').removeClass('febs-hide');
            $('#objectContractor').addClass('febs-hide');

            $('#peccancyContractorName').attr('lay-verify','');
            $('#peccancyRealName').attr('lay-verify','');
            $('#peccancyDept').attr('lay-verify','true');
        }else {
            $('#objectUser').removeClass('febs-hide');
            $('#objectDept').addClass('febs-hide');
            $('#objectContractor').addClass('febs-hide');

            $('#userPeccency').addClass('febs-hide');
            $('#deptPeccency').addClass('febs-hide');
            $('#contractorPeccency').addClass('febs-hide');

            $('#peccancyContractorName').attr('lay-verify','');
            $('#peccancyRealName').attr('lay-verify','true');
            $('#peccancyDept').attr('lay-verify','');
        }
        form.render();
    });

    $deptNameAddBtn.on('click', function () {
        febs.modal.open('选择部门', 'common/dept', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['90%', '85%'],
            data:{
                tenantId:getTenantId(),
                multiple:false,
            },
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                temp = selectData;
                // console.log("获得的部门"+JSON.stringify(temp))
                // debugger
                if(selectData){
                    getPeccancyCount(selectData.deptId,getPeccancyObject())
                    $view.find('input[name="peccancyUserDeptId"]').val(selectData.deptId);
                    $view.find('input[name="peccancyUserDept"]').val(selectData.deptName);
                    $view.find('input[name="peccancyDept"]').val(selectData.deptName);

                    layer.close(index);
                }

            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });

    });


    $contractorAddBtn.on('click', function () {
        febs.modal.open('选择承包队', 'common/contractor', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['90%', '85%'],
            data:{
                tenantId:getTenantId(),
                multiple:false,
            },
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                temp = selectData;
                // console.log("获得的承包队"+JSON.stringify(temp))
                if(selectData){
                    getPeccancyCount(selectData.contractorTeamId,getPeccancyObject())
                    $view.find('input[name="peccancyUserContractorId"]').val(selectData.contractorId);
                    $view.find('input[name="peccancyUserContractorName"]').val(selectData.contractorName);
                    $view.find('input[name="peccancyUserContractorTeamId"]').val(selectData.contractorTeamId);
                    $view.find('input[name="peccancyUserContractorTeamName"]').val(selectData.contractorTeamName);
                    $view.find('input[name="clauseRuleContent"]').val(selectData.clauseRuleContent);
                    $view.find('input[name="peccancyContractorName"]').val(selectData.contractorTeamName);
                    layer.close(index);
                }

            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });

    });


    form.on('checkbox(penaltyAmountFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyAmount"]').val('');
            $view.find('input[name="penaltyAmount"]').attr('disabled',true);
        }
        form.render()
    });

    form.on('checkbox(penaltyScoreFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyScore"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyScore"]').val('');
            $view.find('input[name="penaltyScore"]').attr('disabled',true);
        }
    });

    var adminId = '';
    $peccancyUserSign.on('click', function () {
        adminId = commonJS.UUID(32,16);
        window.open("http://127.0.0.1:8848/CHIC-SAFETY-H5/pages/common/signature.html?adminId=" + adminId)
    });

    $(function() {
        var websocket = null;
        var WEBPATH = "http://172.24.7.112:9080/"
        var websocketPath = "172.24.7.112:9080/websocket/";
        adminId = commonJS.UUID(32,16);
        //判断当前浏览器是否支持WebSocket
        if('WebSocket' in window) {
            websocket = new WebSocket("ws://" + websocketPath + adminId);
        } else {
            alert('当前浏览器不支持扫码上传功能！');
        }
        //连接发生错误的回调方法
        websocket.onerror = function() {
            //					setMessageInnerHTML("WebSocket连接发生错误");
            console.log("WebSocket连接发生错误");
            // getQRCode("二维码生成失败")
        };
        //连接成功建立的回调方法
        websocket.onopen = function() {
            //					setMessageInnerHTML("WebSocket连接成功");
            console.log("WebSocket连接成功");
            getQRCode("/signature.html?adminId=" + adminId);

        }

        //接收到消息的回调方法
        websocket.onmessage = function(event) {

            //					setMessageInnerHTML(event.data);
            console.log(event.data);
            getMessage(JSON.parse(event.data));
        }

        //连接关闭的回调方法
        websocket.onclose = function() {

            //setMessageInnerHTML("WebSocket连接关闭");
            //alert('WebSocket连接关闭');
            console.log("WebSocket连接关闭");
        }

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function() {
            closeWebSocket();
        }

        //关闭WebSocket连接
        function closeWebSocket() {
            websocket.close();
        }

        function setMessageInnerHTML(str) {
            $("#msg").append(str + "</br>");
        }

        function getMessage(obj) {

            let signaturePath = obj.signaturePath

            if(obj.msg == "saveSuccess" && signaturePath) {
                $view.find('input[name="peccancyUserSign"]').val(signaturePath);
                $view.find('#peccancyUserSignImg').removeClass('febs-hide');
                $view.find('#peccancyUserSignImg1').attr('src','/'+signaturePath);
                form.render();
                // 保存模块签名地址
                // commonAjax(WEBPATH + 'hazardReportFile/update', {

                // }, 'post', false, function(result) {
                // 	$('#qrcode').empty().append('<img src="'+ signaturePath +'">')
                // }, false, '');


            }
        }

    });

});